package com.example.administrator.college02.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.college02.BaseFragment;
import com.example.administrator.college02.R;
import com.example.administrator.college02.adapter.CommonAdapter;
import com.example.administrator.college02.adapter.ViewHolder;
import com.example.administrator.college02.model.ItemShudong;
import com.example.administrator.college02.pager.GoodView;
import com.example.administrator.college02.utils.CommonNet;
import com.example.administrator.college02.widget.AppTitleBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/7/15.
 */
public class FragmentShudong extends BaseFragment {

    private CommonAdapter<ItemShudong> mAdapter;
    private List<ItemShudong> mList;
    @BindView(R.id.lv_shudong) ListView lvShudong;
    private Context mContext;
    private GoodView goodView;
    @BindView(R.id.app_title_bar)
    AppTitleBar appTitleBar;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shudong,container,false);
        unbinder = ButterKnife.bind(this,view);
        mContext = getContext();
        getDataFromServer();
        goodView = new GoodView(mContext);
        mAdapter = new CommonAdapter<ItemShudong>(mContext,R.layout.item_shudong) {
            @Override
            public void conver(ViewHolder holder, ItemShudong itemShudong) {
                holder.setText(R.id.item_shudong_title,itemShudong.getTitle());
                holder.setText(R.id.item_shudong_author,itemShudong.getAuthor());
                holder.setText(R.id.item_shudong_lovecount,""+itemShudong.getLovecount());
                holder.setText(R.id.item_shudong_comment,""+itemShudong.getComment());
            }
        };
        appTitleBar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fm.popBackStack();
            }
        });
        lvShudong.setAdapter(mAdapter);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    private void getDataFromServer() {
        StringRequest request = new StringRequest(Request.Method.GET, CommonNet.SHUDONG,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        mList = new ArrayList<>();
                        try {
                            JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject object = jsonArray.getJSONObject(i);
                                Log.d("FragmentShudong",object.toString());
                                String title = object.getString("title");
                                String author = object.getString("author");
                                int lovecount = object.getInt("lovecount");
                                int comment = object.getInt("comment");
                                ItemShudong item = new ItemShudong(title,author,lovecount,comment);
                                mList.add(item);
                            }
                            mAdapter.bindData(mList);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        Volley.newRequestQueue(mContext).add(request);
    }
}
